integer attachment = TRUE;
integer relaychannel = -1812221819;
string cmdname;
key victim;
list gMenu=[];
list statesMenu=[0,0,0,0,0,0,0,0,0,0];
list gAvatarAllow;
integer gMenuLink =1337;
integer gDemon =1338;
integer gPlugin =1339;
integer gVic =1329;
key ToucherID;
integer channel_dialog;
integer listen_id;
string msg ="Posture\nTrigger Animation";
list sideMenu=[" ","Back"," "];


string ANIM = "!p1";

makeMenu()
{
	
	gMenu=[];
	gMenu+=sideMenu;
	if(llList2Integer(statesMenu,0)==1)gMenu+="[x]Posture";
	else gMenu+="[ ]Posture";
	channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
	listen_id = llListen( channel_dialog, "", NULL_KEY, "");
	llDialog(ToucherID, msg,gMenu, channel_dialog);
}

llLRP(integer element_new, integer placeinlist)
{
	statesMenu = llListReplaceList(statesMenu, [element_new], placeinlist, placeinlist);
}

default {
    state_entry() 
    {

    }
    
    run_time_permissions(integer perms) 
    {
        if(perms & (PERMISSION_TRIGGER_ANIMATION)) 
        {
            llSetTimerEvent(0.1);
        }
    }
    
    timer() 
    {
        llStartAnimation(ANIM);
    }
    
    listen(integer channel, string name, key id, string message)
    {
    	
    	llListenRemove(listen_id);
    	if(message=="[x]Posture")
    	{
    		llSetTimerEvent(0.0);
    		llStopAnimation(ANIM);
    		llLRP(0,0);
    		makeMenu();
    	}
		else if(message=="[ ]Posture")
		{
			llRequestPermissions(llGetOwner(), PERMISSION_TRIGGER_ANIMATION);
			llSetTimerEvent(0.1);
			llLRP(1,0);
			makeMenu();
		}
		else if(message=="Back")
		{
			llMessageLinked(LINK_THIS, gMenuLink, "DV_MAIN_MISC", ToucherID);
		}
    }
    
    link_message(integer sender, integer num, string str, key id)
  	{
  		
  		if(num ==gMenuLink)
  		{
  			if(str==llGetScriptName())
  			{
  				llMessageLinked(LINK_THIS, gDemon, llGetScriptName(), NULL_KEY);
  				ToucherID=id;
  				makeMenu();
  			}
  		}
  		if(num ==gDemon)
  		{
  			if(str =="attachment")attachment=TRUE;
  			else if(str =="device")attachment=FALSE;
  			else if(str!=llGetScriptName())
  			{
				llListenRemove(listen_id);
  			}
  		}
  		if(num==gPlugin)
  		{
  			if(str=="ON_REZ")
  			{
  				
  			}
  			if(str=="RESET")
  			{
  				statesMenu=[0,0,0,0,0,0,0,0,0,0];  				
  			}
  		}
  		if(num==gVic)
  		{
  				victim=(key)str;
  				cmdname = (string)llGetKey();
  		}
  	}
}
